package org.yxing.palpalgps.activity;

import java.util.ArrayList;

import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.domain.AsyncImageLoader.ImageCallback;
import org.yxing.palpalgps.utils.Helper;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class UserPhotoViewActivity extends AbstractActivity implements OnClickListener {
    private ArrayList<String> data;
    private int id;
    
    private ImageView mImageView;
    private Button prevButton;
    private Button nextButton;
    private TextView currentNumView;
    
    private float displayWidth;
    
    private AsyncImageLoader mAsyncImageLoader;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_view);
        setTitle(getString(R.string.photo_view));
        
        //get data
        data = getIntent().getStringArrayListExtra("data");
        //get id
        id = getIntent().getIntExtra("id", 0);
        if (id >= data.size())
            id = data.size() - 1;
        
        mImageView = (ImageView) findViewById(R.id.photoView);
        currentNumView = (TextView) findViewById(R.id.currentNum);
        prevButton = (Button) findViewById(R.id.buttonPrev);
        nextButton = (Button) findViewById(R.id.buttonNext);
        prevButton.setOnClickListener(this);
        nextButton.setOnClickListener(this);
        
        Display display = getWindowManager().getDefaultDisplay();
        displayWidth = display.getWidth();
        
        ((TextView) findViewById(R.id.count)).setText(Integer.toString(data.size()));
        
        showPhoto();
    }
    
    /**
     * show photo
     */
    private void showPhoto() {
        pd.show();
        String pid = data.get(id);
        currentNumView.setText(Integer.toString(id + 1));
        if (mAsyncImageLoader == null)
            mAsyncImageLoader = new AsyncImageLoader();
        
        mAsyncImageLoader.loadDrawable(Helper.getPictureUrl(pid), mImageView, imageCallback);
    }
    
    private ImageCallback imageCallback = new ImageCallback() {

        public void imageLoaded(Drawable imageDrawable, ImageView imageView, String imageUrl) {
            //drawable to bitmap
            BitmapDrawable bd = (BitmapDrawable) imageDrawable;
            Bitmap bmp = bd.getBitmap();
            
            //get scale
            int width = bmp.getWidth();
            float scale = displayWidth/width;
            
            //create matrix
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            
            //get new bitmap
            Bitmap newBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
            
            
            imageView.setImageBitmap(newBitmap);
            pd.cancel();
        }
    };

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.buttonPrev:
            id--;
            if (id < 0)
                id = 0;
            break;
        case R.id.buttonNext:
            id ++;
            if (id >= data.size())
                id = data.size() - 1;
            break;
        }
        showPhoto();
    }
}
